require.config({
    baseUrl: "../Cart_Modules",
    paths: {
        jq: "../libs/jquery",
        ajax:"../libs/ajax",
        getData: "../modules/GetData",
        render: "Render_Cart",
        updateLocal: "UpdateLocal",
        allCheck: "AllCheck",
        totalNum:"TotalNum",
        totalPrice: "TotalPrice",
        delCheck: "DelCheck",
    }
})

require(["jq", "ajax", "getData", "render", "updateLocal", "allCheck", "totalNum", "totalPrice", "delCheck", ], function (_, ajax, getData, render, updateLocal, allCheck, totalNum, totalPrice, delCheck) {

    const url = "http://localhost:3000/api1?type=getGoods";

    let goods = localStorage.getItem("goods") ? JSON.parse(localStorage.getItem("goods")) : [];

    getData(url).then((res) => {
        console.log(res);

        render(res, goods, $("tbody"));
        console.log(goods);

    // 去结算样式，鼠标按下与松开
        let btn_disabled = document.getElementsByClassName("btn-disabled")[0];
        btn_disabled.onmousedown = function (even) {
            if (even.button == '0') {
                btn_disabled.className += ' btn';
            }
        }
        btn_disabled.onmouseup = function (even) {
            if (even.button == '0') {
                btn_disabled.className = 'btn-disabled';
            }
        }

    // 商品的选中状态
        $("tbody").on("change", ".s_check", function () {
            const id = $(this).parent().parent().parent().attr("id");
            let c = this.checked;

            updateLocal(goods, id, function (i) {
                goods[i].check = c;

                // if (goods[i].check === true) {
                //     $(".no-select-tip").get(0).style.display = "none";
                //     $(".btn-disabled").addClass('btn-primary');
                // } else {
                //     $(".no-select-tip").get(0).style.display = "block";
                //     $(".btn-disabled").removeClass('btn-primary');
                // }
            })

            $("#allCheck")[0].checked = allCheck(goods);

            $(".total-Num").html(totalNum(goods));
            $(".total-Price").html(totalPrice(goods));
        })

    // 商品的修改数量
        $("tbody").on("input", ".goods-num", function () {
            const id = $(this).parent().parent().parent().attr("id");

            const goods_Num = this.value - 0;
            const goods_Sum = $(this).parent().parent().prev().children(".singleGoods_price").html() - 0;
            $(this).parent().parent().next().children("span").html(goods_Num * goods_Sum);

            updateLocal(goods, id, function (i) {
                goods[i].sum = goods_Num;
            })

            $(".total-Num").html(totalNum(goods));
            $(".total-Price").html(totalPrice(goods));
        })

    // 渲染总价数字改变
        $("tbody").on("click", ".icon-plus", function () {
            let id = $(this).parent().parent().parent().attr("id");
            let Unit_Price = $(this).parent().parent().prev().children("span").html() - 0;
            let set_Num = $(this).prev().val() - 0;
            set_Num++;
            if (set_Num > 30) set_Num = 30;
            $(this).prev().val(set_Num);            // 商品数量增加
            $(this).parent().parent().next().children("span").html(Unit_Price * set_Num)
            updateLocal(goods, id, function (i) {
                goods[i].num = set_Num;
            })

            $(".total-Num").html(totalNum(goods));
            $(".total-Price").html(totalPrice(goods));
        })

    // 渲染总价数字改变
        $("tbody").on("click", ".icon-reduce", function () {
            let id = $(this).parent().parent().parent().attr("id");
            let Unit_Price = $(this).parent().parent().prev().children("span").html() - 0;
            let set_Num = $(this).next().val() - 0;
            set_Num--;
            if (set_Num < 0) set_Num = 0;
            $(this).next().val(set_Num);
            $(this).parent().parent().next().children("span").html(Unit_Price * set_Num);
            updateLocal(goods, id, function (i) {
                goods[i].num = set_Num;
            })

            $(".total-Num").html(totalNum(goods));
            $(".total-Price").html(totalPrice(goods));
        })

    // 商品的删除
        $("tbody").on("click", ".del", function () {
            const id = $(this).parent().parent().attr("id");

            if (confirm('确认删除所选商品吗？')) {
                $(this).parent().parent().remove();
            }

            updateLocal(goods, id, function (i) {
                goods.splice(i, 1);
            })

            $(".total-Num").html(totalNum(goods));
            $(".total-Price").html(totalPrice(goods));
        })

    // 单个选中决定是否全选
        $("#allCheck")[0].checked = allCheck(goods);

    // 全选影响单个商品的选中
        $("#allCheck").change(function () {
            // console.log(this.checked);
            goods.forEach(val => {
                val.check = this.checked;

                // if (val.check === true) {
                //     $(".no-select-tip").get(0).style.display = "none";
                //     $(".btn-disabled").addClass('btn-primary');
                // } else {
                //     $(".no-select-tip").get(0).style.display = "block";
                //     $(".btn-disabled").removeClass('btn-primary');
                // }
            })
            localStorage.setItem("goods", JSON.stringify(goods));

            for (let i = 0; i < $(".s_check").length; i++){
            /* 第一种方法 */
            // $(".s_check").eq(i)[0].checked = this.checked
            /* 第二种方法 */
                $(".s_check")[i].checked = this.checked
            }

            $(".total-Num").html(totalNum(goods));

            $(".total-Price").html(totalPrice(goods));
        })

    // 计算总数量（几件）
        $(".total-Num").html(totalNum(goods));
        
    // 计算总价
        $(".total-Price").html(totalPrice(goods));

    // 删除选中商品
        $(".batch_Delete").click(function () {
            goods = delCheck(goods);
            render(res, goods, $("tbody"));
            $(".total-Num").html(0);
            $(".total-Price").html(0);
            $("#allCheck")[0].checked = allCheck(goods);
        })
    })
})